﻿using AMEE.TcpComm;
using System;
using System.Diagnostics;
using System.Threading;

namespace TCPTestServer
{
    class Program
    {

        static void Main(string[] args) {
            /* Create a listener that outputs to the console screen, and 
             * add it to the debug listeners. */
            TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
            Debug.Listeners.Add(myWriter);

            //TcpServer server = new TcpServer(13000);
            //server.DataProcessor = clntMsg => "$:Done;";
            //server.Start();
            //Thread.Sleep(1000);

            //Thread clnt = new Thread(p => Program.client(p)) { Name = "Client Thread" };
            //clnt.Start(13000);

            //Thread.Sleep(5000);
            //Console.WriteLine("Stopping Server");
            //server.Stop();

            Func<string, string> processor = clntMsg => {
                Thread.Sleep(1000);
                return "$:Done;";
            };

            int srvCnt = TCPTestServer.Properties.Settings.Default.NumServer1to4;
            TcpServer[] servers = new TcpServer[srvCnt];

            String ip = Properties.Settings.Default.IP;
            var server = new TcpServer(Properties.Settings.Default.Port);
            server.DataProcessor = processor;
            servers[0] = server;
            if (srvCnt >= 2) {
                var server2 = new TcpServer(Properties.Settings.Default.Port2);
                server2.DataProcessor = processor;
                servers[1] = server2;
            }
            if (srvCnt >= 3) {
                var server3 = new TcpServer(Properties.Settings.Default.Port3);
                server3.DataProcessor = processor;
                servers[2] = server3;
            }
            if (srvCnt >= 4) {
                var server4 = new TcpServer(Properties.Settings.Default.Port4);
                server4.DataProcessor = processor;
                servers[3] = server4;
            }
            foreach (var svr in servers) {
                svr.Start();
            }


            //for (int i = 0; i < servers.Length; i++) {
            //    Thread clnt = new Thread(p => Program.client(p)) { Name = "Client Thread" };
            //    clnt.Start(13000 + i);
            //}


            ConsoleKey key;
            do {
                Console.WriteLine("Drücke Escape zum Beenden.");
                Console.WriteLine("Drücke die Zahlen 1 bis 4 zum Schließen eines spezifischen Servers");
                key = Console.ReadKey().Key;
                switch (key) {
                    case ConsoleKey.NumPad1:
                    // weiter
                    case ConsoleKey.D1:
                        servers[0].Stop();
                        Console.WriteLine("\nServer 1 gestoppt.");
                        break;
                    case ConsoleKey.NumPad2:
                    // weiter
                    case ConsoleKey.D2:
                        servers[1].Stop();
                        Console.WriteLine("\nServer 2 gestoppt.");
                        break;
                    case ConsoleKey.NumPad3:
                    // weiter
                    case ConsoleKey.D3:
                        servers[2].Stop();
                        Console.WriteLine("\nServer 3 gestoppt.");
                        break;
                    case ConsoleKey.NumPad4:
                    // weiter
                    case ConsoleKey.D4:
                        servers[3].Stop();
                        Console.WriteLine("\nServer 4 gestoppt.");
                        break;
                    default:
                        break;
                }
            } while (key != ConsoleKey.Escape);
            Console.WriteLine("\nStoppe Server");
            foreach (var svr in servers) {
                svr.Stop();
            }
            for (int i = 10; i > 0; i--) {
                Console.Write(" {0} ", i);
                System.Threading.Thread.Sleep(1000);
            }
            Console.WriteLine("\n press any key to quit");
            Console.ReadKey();
        }

        static void client(object port) {
            TcpWriter writer = new TcpWriter("127.0.0.1", (int)port);
            Console.WriteLine("Client connected [{0}]", writer.Connected);
            Console.WriteLine("Close after sent [{0}]", writer.CloseAfterSent);
            writer.MessageSent += (sender, e) => Console.WriteLine("\n\t Nachricht gesendet.");
            writer.ResponseReceived += (sender, ev) => Console.WriteLine("Antwort 1 erhalten \"{0}\".", ev.Response);
            if (writer.Connected) {
                writer.Send("Meine erste Nachricht (async).");
                System.Threading.Thread.Sleep(1000);
                string resp = writer.SendBlocking("Meine zweite Nachricht (blocking).");
                Console.WriteLine("Antwort 2 erhalten \"{0}\".", resp);
            }
        }

    }
}
